home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 July: Mac OS SDK / Dev.CD Jul 96 SDK / Dev.CD Jul 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc Development Framework / ODFDev / Draw / Sources / DrwDDCmd.cpp < prev    next >
Encoding:
Text File  |  1996-04-25  |  14.1 KB  |  448 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                DrwDDCmd.cpp
  4. //    Release Version:    $ ODF 1 $
  5. //
  6. //    Author:                Mary Boetcher
  7. //
  8. //    Copyright:    (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  9. //
  10. //========================================================================================
  11.  
  12. #include "ODFDraw.hpp"
  13.  
  14. #ifndef DRWDDCMD_H
  15. #include "DrwDDCmd.h"
  16. #endif
  17.  
  18. #ifndef DRAWCONT_H
  19. #include "DrawCont.h"
  20. #endif
  21.  
  22. #ifndef DRAWSEL_H
  23. #include "DrawSel.h"
  24. #endif
  25.  
  26. #ifndef BASESHP_H
  27. #include "BaseShp.h"
  28. #endif
  29.  
  30. #ifndef DRAWPART_H
  31. #include "DrawPart.h"
  32. #endif
  33.  
  34. #ifndef DRAWCLIP_H
  35. #include "DrawClip.h"
  36. #endif
  37.  
  38. #ifndef DRAWLINK_H
  39. #include "DrawLink.h"
  40. #endif
  41.  
  42. // ----- FrameWork Includes -----
  43.  
  44. #ifndef FWFRAME_H
  45. #include "FWFrame.h"
  46. #endif
  47.  
  48. #ifndef FWPRESEN_H
  49. #include "FWPresen.h"
  50. #endif
  51.  
  52. //========================================================================================
  53. // RunTime Info
  54. //========================================================================================
  55.  
  56. #ifdef FW_BUILD_MAC
  57. #pragma segment odfdrawcommand
  58. #endif
  59.  
  60. FW_DEFINE_AUTO(CDrawDragCommand)
  61. FW_DEFINE_AUTO(CDrawDropCommand)
  62.  
  63. //========================================================================================
  64. // class CDrawDragCommand
  65. //========================================================================================
  66.  
  67. //----------------------------------------------------------------------------------------
  68. //    CDrawDragCommand constructor
  69. //----------------------------------------------------------------------------------------
  70. CDrawDragCommand::CDrawDragCommand(Environment* ev,
  71.                                    CDrawPart* part,
  72.                                    FW_CFrame* frame,
  73.                                    CDrawSelection* selection,
  74.                                    FW_Boolean canUndo)
  75.     : FW_CDragCommand(ev, frame, canUndo),
  76.         fDrawPart(part),
  77.         fDrawSelection(selection),
  78.         fDraggedContent(NULL)
  79. {
  80. }
  81.  
  82. //----------------------------------------------------------------------------------------
  83. //    CDrawDragCommand destructor
  84. //----------------------------------------------------------------------------------------
  85.  
  86. CDrawDragCommand::~CDrawDragCommand()
  87. {
  88.     delete fDraggedContent;        // Will call remove all if necessary
  89.     fDraggedContent = NULL;
  90. }
  91.  
  92. //----------------------------------------------------------------------------------------
  93. //    CDrawDragCommand::SaveUndoState
  94. //----------------------------------------------------------------------------------------
  95.  
  96. void CDrawDragCommand::SaveUndoState(Environment* ev)    // Override
  97. {
  98.     FW_ASSERT(fDraggedContent == NULL);    
  99.     fDraggedContent = FW_NEW(CDrawContent, (ev, fDrawSelection->GetDrawContent(ev)));
  100. }
  101.  
  102. //----------------------------------------------------------------------------------------
  103. //    CDrawDragCommand::UndoIt
  104. //----------------------------------------------------------------------------------------
  105.  
  106. void CDrawDragCommand::UndoIt(Environment* ev)    // Override
  107. {
  108.     // ----- Add the dragged shapes back into the part
  109.     CDrawContentShapeIterator ite(fDraggedContent);
  110.     for (CBaseShape* shape = ite.First(); ite.IsNotComplete(); shape = ite.Next())
  111.     {
  112.         shape->RestoreShape(ev, fDrawPart);
  113.     }
  114.  
  115.     // ----- Add the saved shapes to the selection
  116.     fDrawSelection->SelectContent(ev, fDraggedContent);
  117.     
  118.     // ----- Invalidate 
  119.     GetPresentation(ev)->Invalidate(ev, fDrawSelection->GetUpdateShape());
  120. }
  121.  
  122. //----------------------------------------------------------------------------------------
  123. //    CDrawDragCommand::RedoIt
  124. //----------------------------------------------------------------------------------------
  125.  
  126. void CDrawDragCommand::RedoIt(Environment* ev)    // Override
  127. {
  128.     // Select saved shapes
  129.     fDrawSelection->SelectContent(ev, fDraggedContent);
  130.  
  131.     fDrawSelection->ClearSelection(ev); // clear them, again
  132. }
  133.  
  134. //----------------------------------------------------------------------------------------
  135. //    CDrawDragCommand::FreeUndoState
  136. //----------------------------------------------------------------------------------------
  137.  
  138. void CDrawDragCommand::FreeUndoState(Environment *ev)    // Override
  139. {
  140.     // Delete the dragged shapes - they're gone forever
  141.     CBaseShape* shape;
  142.  
  143.     //-- First check whether the shapes are promised
  144.     CDrawContentShapeIterator ite(fDraggedContent);
  145.     for (shape = ite.First(); ite.IsNotComplete(); shape = ite.Next())
  146.     {
  147.         shape->CheckPromise(ev, fDrawPart);
  148.     }
  149.  
  150.     //-- Now delete them
  151.     while ((shape = fDraggedContent->GetFirstShape()) != NULL)
  152.     {
  153.         fDraggedContent->RemoveShape(ev, shape);
  154.         delete shape;
  155.     }
  156. }
  157.  
  158. //========================================================================================
  159. // class CDrawDropCommand
  160. //========================================================================================
  161.  
  162. //----------------------------------------------------------------------------------------
  163. //    CDrawDropCommand constructor
  164. //----------------------------------------------------------------------------------------
  165. CDrawDropCommand::CDrawDropCommand(Environment* ev,
  166.                                    CDrawPart* part,
  167.                                    FW_CFrame* frame,
  168.                                    ODDragItemIterator* dropInfo, 
  169.                                    ODFacet* facet, 
  170.                                    const FW_CPoint& windowPoint,
  171.                                    FW_Boolean canUndo)
  172.     : FW_CDropCommand(ev, frame, dropInfo, facet, windowPoint, canUndo),
  173.         fDrawPart(part),
  174.         fSavedLink(NULL),
  175.         fDropDelta(FW_kZeroPoint),
  176.         fDroppedContent(NULL),
  177.         fDrawSelection((CDrawSelection*)GetPresentation(ev)->GetSelection(ev))
  178. {
  179. }
  180.  
  181. //----------------------------------------------------------------------------------------
  182. //    CDrawDropCommand destructor
  183. //----------------------------------------------------------------------------------------
  184.  
  185. CDrawDropCommand::~CDrawDropCommand()
  186. {
  187.     delete fDroppedContent;
  188. }
  189.  
  190. //----------------------------------------------------------------------------------------
  191. //    CDrawDropCommand::DoDrop
  192. //----------------------------------------------------------------------------------------
  193. FW_Boolean CDrawDropCommand::DoDrop(Environment* ev, 
  194.                                 ODStorageUnit* dropSU, 
  195.                                 const FW_CPoint& mouseDownOffset, 
  196.                                 const FW_CPoint& dropPoint,
  197.                                 FW_Boolean isDropMove,
  198.                                 short itemNumber)
  199. {
  200.     if (itemNumber == 1)
  201.         fDrawSelection->CloseSelection(ev);
  202.     
  203.     FW_Boolean result = FW_CDropCommand::DoDrop(ev, dropSU, mouseDownOffset, dropPoint, isDropMove, itemNumber);
  204.     
  205.     if (result)
  206.     {    
  207.         fDrawPart->SetTool(ev, kSelectTool);
  208.         
  209.         fDrawSelection->CalcCache(ev);
  210.         
  211.         FW_CPoint newPosition(dropPoint.x - mouseDownOffset.x, dropPoint.y - mouseDownOffset.y);
  212.         
  213.         FW_CRect box;
  214.         fDrawSelection->GetDragRect(box);
  215.  
  216.         fDrawSelection->OffsetSelection(ev, newPosition.x - box.left, newPosition.y - box.top);
  217.         
  218.         ODShape* updateShape = fDrawSelection->GetUpdateShape();
  219.         
  220.         // ----- Calculate clip -----
  221.         CDrawFacetClipper facetClipper(ev, fDrawPart);
  222.         facetClipper.Clip(ev, fDrawSelection->GetPresentation(ev), updateShape);    
  223.  
  224.         // ----- Invalidate -----
  225.         GetPresentation(ev)->Invalidate(ev, updateShape);
  226.         
  227.         // ----- If I am not the active part I should not have a selection -----
  228.         if (!fDrawPart->HasSelectionFocus(ev))
  229.             fDrawSelection->CloseSelection(ev);
  230.     }
  231.     
  232.     return result;
  233. }
  234.  
  235. //----------------------------------------------------------------------------------------
  236. //    CDrawDropCommand::DoDroppedInSameFrame
  237. //----------------------------------------------------------------------------------------
  238.  
  239. FW_Boolean CDrawDropCommand::DoDroppedInSameFrame(Environment* ev, 
  240.                                               ODStorageUnit* dropSU, 
  241.                                               const FW_CPoint& mouseDownOffset, 
  242.                                               const FW_CPoint& dropPoint)
  243. {
  244.     FW_UNUSED(dropSU);
  245.     
  246.     FW_CPoint newPosition(dropPoint.x - mouseDownOffset.x, dropPoint.y - mouseDownOffset.y);
  247.  
  248.     FW_CRect box;
  249.     fDrawSelection->GetDragRect(box);
  250.  
  251.     fDropDelta.Set(newPosition.x - box.left, newPosition.y - box.top);
  252.     
  253.     this->OffsetSelection(ev, fDropDelta);
  254.  
  255.     return TRUE;
  256. }
  257.  
  258. //----------------------------------------------------------------------------------------
  259. //    CDrawDropCommand::SaveRedoState
  260. //----------------------------------------------------------------------------------------
  261.  
  262. void CDrawDropCommand::SaveRedoState(Environment* ev)    // Override
  263. {
  264.     if (fSavedLink == NULL)    // if a link was created, don't need to save anything
  265.     {
  266.         // Save shapes that were just dropped
  267.         FW_ASSERT(fDroppedContent == NULL);
  268.         fDroppedContent = FW_NEW(CDrawContent, (ev, fDrawSelection->GetDrawContent(ev)));
  269.     }
  270. }
  271.  
  272. //----------------------------------------------------------------------------------------
  273. //    CDrawDropCommand::UndoIt
  274. //----------------------------------------------------------------------------------------
  275. void CDrawDropCommand::UndoIt(Environment* ev)    // Override
  276. {
  277.     if (fSavedLink)
  278.     {
  279.         GetDrawLinkManager(ev)->UndoPasteAs(ev, fSavedLink);
  280.     }
  281.     else if (this->IsDragMoveInSameFrame(ev))    // it's a drag-move
  282.     {
  283.         // select dropped shapes and move them back to the original position
  284.         fDrawSelection->SelectContent(ev, fDroppedContent);
  285.         this->OffsetSelection(ev, -fDropDelta);
  286.         fDrawSelection->SelectionChanged(ev);    // need more than this for redraw???
  287.     }
  288.     else
  289.     {
  290.         // select dropped shapes and remove them from the document
  291.         fDrawSelection->SelectContent(ev, fDroppedContent);
  292.         fDrawSelection->ClearSelection(ev);
  293.     }
  294. }
  295.  
  296. //----------------------------------------------------------------------------------------
  297. //    CDrawDropCommand::RedoIt
  298. //----------------------------------------------------------------------------------------
  299. void CDrawDropCommand::RedoIt(Environment* ev)    // Override
  300. {
  301.     if (fSavedLink)
  302.     {
  303.         GetDrawLinkManager(ev)->RedoPasteAs(ev, fSavedLink);
  304.         this->OffsetSelection(ev, -fDropDelta);
  305.     }
  306.     else if (this->IsDragMoveInSameFrame(ev))    // it's a drag-move
  307.     {
  308.         // select dropped shapes and move them to new position
  309.         fDrawSelection->SelectContent(ev, fDroppedContent);
  310.         this->OffsetSelection(ev, fDropDelta);
  311.     }
  312.     else    // dropped shapes are copies
  313.     {
  314.         // add dropped shapes back into document
  315.         this->RestoreDroppedShapes(ev);
  316.         GetPresentation(ev)->Invalidate(ev, fDrawSelection->GetUpdateShape());
  317.     }
  318.     fDrawSelection->SelectionChanged(ev);
  319. }
  320.  
  321. //----------------------------------------------------------------------------------------
  322. //    CDrawDropCommand::RestoreDroppedShapes
  323. //----------------------------------------------------------------------------------------
  324. void CDrawDropCommand::RestoreDroppedShapes(Environment* ev)
  325. {
  326.     // Add the dropped shapes back into the part
  327.     CDrawContentShapeIterator ite(fDroppedContent);
  328.     for (CBaseShape* shape = ite.First(); ite.IsNotComplete(); shape = ite.Next())
  329.     {
  330.         shape->RestoreShape(ev, fDrawPart);
  331.     }
  332.  
  333.     // Add the saved shapes to the selection
  334.     fDrawSelection->SelectContent(ev, fDroppedContent);
  335. }
  336.  
  337. //----------------------------------------------------------------------------------------
  338. //    CDrawDropCommand::OffsetSelection
  339. //----------------------------------------------------------------------------------------
  340.  
  341. void CDrawDropCommand::OffsetSelection(Environment* ev, const FW_CPoint& delta)
  342. {
  343.     if (delta != FW_kZeroPoint)
  344.     {
  345.         GetPresentation(ev)->Invalidate(ev, fDrawSelection->GetUpdateShape());
  346.         fDrawSelection->OffsetSelection(ev, delta.x, delta.y);
  347.         GetPresentation(ev)->Invalidate(ev, fDrawSelection->GetUpdateShape());
  348.  
  349.         // Adjust offsets of linked shapes
  350.         CDrawContentShapeIterator it(fDrawSelection->GetDrawContent(ev));
  351.         for (CBaseShape* shape = it.First(); it.IsNotComplete(); shape = it.Next())
  352.         {
  353.             if (shape->GetSubscribeLink() != NULL)
  354.             {
  355.                 shape->GetSubscribeLink()->AdjustUpdateOffset(ev, delta);
  356.             }
  357.         }
  358.     }
  359. }
  360.  
  361. //----------------------------------------------------------------------------------------
  362. //    CDrawDropCommand::CommitUndone
  363. //----------------------------------------------------------------------------------------
  364.  
  365. void CDrawDropCommand::CommitUndone(Environment* ev)    // Override
  366. {
  367.     if (fSavedLink == NULL)
  368.         FW_CDropCommand::CommitUndone(ev);
  369. }
  370.  
  371. //----------------------------------------------------------------------------------------
  372. //    CDrawDropCommand::FreeRedoState
  373. //----------------------------------------------------------------------------------------
  374.  
  375. void CDrawDropCommand::FreeRedoState(Environment* ev)    // Override
  376. {
  377.     //-- Delete the the dropped shapes
  378.     CBaseShape* shape;
  379.     while ((shape = fDroppedContent->GetFirstShape()) != NULL)
  380.     {
  381.         fDroppedContent->RemoveShape(ev, shape);
  382.         delete shape;
  383.     }
  384. }
  385.  
  386. //----------------------------------------------------------------------------------------
  387. //    CDrawDropCommand::GetDrawLinkManager
  388. //----------------------------------------------------------------------------------------
  389.  
  390. CDrawLinkManager* CDrawDropCommand::GetDrawLinkManager(Environment* ev) const
  391. {
  392.     return (CDrawLinkManager*) fDrawPart->GetLinkManager(ev);
  393. }
  394.  
  395. //----------------------------------------------------------------------------------------
  396. //    CDrawDropCommand::DoDroppedPasteAs
  397. //----------------------------------------------------------------------------------------
  398.  
  399. void CDrawDropCommand::DoDroppedPasteAs(Environment* ev, 
  400.                                     const FW_CPoint& mouseDownOffset, 
  401.                                     const FW_CPoint& dropPoint)
  402. {
  403.     fDropDelta = dropPoint - mouseDownOffset;
  404.     fSavedLink = (CDrawSubscribeLink*) this->GetNewLink(ev);
  405.  
  406.     if (fSavedLink)
  407.     {
  408.         // Save information about newly-created link
  409.         fSavedLink->SetUpdateOffset(ev, fDropDelta);
  410.     
  411.         if (fDropDelta != FW_kZeroPoint)
  412.         {
  413.             // invalidate and clear the current selection
  414.             GetPresentation(ev)->Invalidate(ev, fDrawSelection->GetUpdateShape());
  415.             fDrawSelection->CloseSelection(ev);    // empty the selection
  416.     
  417.             // select the dropped shapes
  418.             CDrawLinkManager* linkMgr = GetDrawLinkManager(ev);
  419.             linkMgr->SelectSubscribedShapes(ev, fSavedLink);
  420.     
  421.             // offset the dropped shapes
  422.             fDrawSelection->OffsetSelection(ev, fDropDelta.x, fDropDelta.y);
  423.             GetPresentation(ev)->Invalidate(ev, fDrawSelection->GetUpdateShape());
  424.         }
  425.     }
  426.     else    // no link created - must be Embed As
  427.     {
  428.         /* Code copied from DoDrop */
  429.         fDrawPart->SetTool(ev, kSelectTool);
  430.         
  431.         fDrawSelection->CalcCache(ev);
  432.         
  433.         FW_CRect box;
  434.         fDrawSelection->GetDragRect(box);
  435.  
  436.         fDrawSelection->OffsetSelection(ev, fDropDelta.x - box.left, fDropDelta.y - box.top);
  437.         
  438.         ODShape* updateShape = fDrawSelection->GetUpdateShape();
  439.         
  440.         // ----- Calculate clip -----
  441.         CDrawFacetClipper facetClipper(ev, fDrawPart);
  442.         facetClipper.Clip(ev, fDrawSelection->GetPresentation(ev), updateShape);    
  443.  
  444.         // ----- Invalidate -----
  445.         GetPresentation(ev)->Invalidate(ev, updateShape);
  446.     }
  447. }
  448.